import type {ResponseData, ResponsePaginationData} from '@/utils/request';
import {SUCCESS_CODE} from '@/utils/request';
import type {RequestData} from '@ant-design/pro-table';
import {message} from 'antd';
import type {DeviceWarning, QueryListParams, TableListItem, WarningEvent} from './data';
import {request} from 'umi';


/**
 * 获取设备列表
 * @param params
 */
export async function findDeviceWarningList(params: QueryListParams): Promise<RequestData<TableListItem>> {
  const res: ResponsePaginationData<any> = await request(`/device/warning`, {
    params: {
      page: params.current,
      pageSize: params.pageSize,
      deviceNo: params.deviceNo,
      startTime: params.startTime,
      endTime: params.endTime,
      status: params.status,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return {success: false, total: 0, data: []};
  }

  return {
    success: true,
    total: res.data.total,
    data: res.data.list.map(item => ({
      ...item,
    })),
  };
}

interface DeviceWarningEventQuery extends Pagination {
  deviceNo?: string;
  warningId: string;
}

/**
 * 获取设备列表
 * @param params
 */
export async function findDeviceWarningEventList(params: DeviceWarningEventQuery): Promise<RequestData<WarningEvent>> {
  console.assert(params.deviceNo);
  console.assert(params.warningId);
  const res: ResponsePaginationData<any> = await request(`/device/warning/event`, {params});
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return {success: false, total: 0, data: []};
  }

  return {
    success: true,
    total: res.data.total,
    data: res.data.list.map(item => ({
      ...item,
      no: String(item.id),
    })),
  };
}

export async function confirmDeviceWarning(deviceWarning: DeviceWarning): Promise<ResponseData<any>> {
  const res = await request('/device/warning/confirm', {
    method: 'POST',
    params: {
      id: deviceWarning.id,
    },
  });
  if (res.code === SUCCESS_CODE) {
    message.success('操作成功');
  } else {
    message.error('操作失败');
  }
  return res;
}
